import 'package:flutter/material.dart';
import 'package:gin_vue_admin_web/exports.dart';
import 'package:gin_vue_admin_web/models/models.dart';
import 'package:gin_vue_admin_web/providers/system/system_riverpod.dart';
import 'package:gin_vue_admin_web/theme/exports_theme.dart';
import 'package:gin_vue_admin_web/util/export_util.dart';
import 'package:gin_vue_admin_web/widgets/export_widget.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

import '../../layout/export_layout.dart';
import '../../router/router.dart';
import '../../widgets/expandable_tree_view.dart';

class MenuPage extends StatefulWidget {
  const MenuPage({super.key});

  @override
  State<MenuPage> createState() => _MenuPageState();
}

class _MenuPageState extends State<MenuPage> {
  @override
  void dispose() {
    super.dispose();
  }

  TreeTableModel<DataMenusItem> toElement2(DataMenusItem e,
      {int depth = 0, int parentId = 0}) {
    return TreeTableModel<DataMenusItem>(
        data: e,
        parentId: parentId,
        children: e.children?.isNotEmpty == true
            ? e.children!
                .map((item) =>
                    toElement2(item, depth: depth + 1, parentId: e.id))
                .toList()
            : [],
        id: e.id,
        depth: depth);
  }

  final titles = const [
    TitleData(title: "ID", key: "id", width: 60),
    TitleData(title: "父节点", key: "pid", width: 80),
    TitleData(title: "展示名字", key: "title"),
    TitleData(title: "路由Name", key: "name"),
    TitleData(title: "路由Path", key: "path"),
    TitleData(title: "是否隐藏", key: "hidden"),
    TitleData(title: "排序", key: "sort", width: 60),
    TitleData(title: "组件路径", key: "component"),
    TitleData(title: "操作", key: "buttons", width: 150),
  ];
  final buttons = [
    (title: "编辑", icon: Icons.edit),
    (title: "删除", icon: Icons.delete),
  ];

  @override
  Widget build(BuildContext context) {
    final style = TextStyles.getBodyStyle(context);
    return DefaultLayout(
      route: RoutePath.menu,
      child: HookConsumer(
        builder: (BuildContext context, WidgetRef ref, Widget? child) {
          return AsyncValueWidget(ref.watch(getMenusProvider),
              builder: (value) {
            final list = value as List<DataMenusItem>;
            final nodes = list.map(toElement2).toList();
            return TreeTable<DataMenusItem>(
              data: nodes,
              titles: titles,
              cellBuilder: (DataGridCell e) {
                if (e.columnName == "buttons") {
                  return Builder(builder: (context) {
                    return Center(
                      child: OverflowBar(
                        children: buttons
                            .map((e) => TextButtonWithIconX(
                                  onPressed: () {
                                    Scaffold.of(context).openEndDrawer();
                                  },
                                  label: Text(e.title),
                                  style: context
                                      .appExtensionTheme?.textButtonStyle,
                                  icon: Icon(
                                    e.icon,
                                    size: 20,
                                  ),
                                ))
                            .toList(),
                      ),
                    );
                  });
                } else if (e.columnName == "hidden") {
                  return Center(
                    child: Text(
                      e.value == true ? "隐藏" : "显示"
                    ),
                  );
                }
                return Center(child: Text("${e.value}"));
              },
              builder: (List<TreeTableModel<DataMenusItem>> items) {
                return items
                    .map((e) => DataGridRow(cells: [
                          DataGridCell<TreeTableModel<DataMenusItem>>(
                              columnName: 'id', value: e),
                          DataGridCell<int>(
                              columnName: 'pid', value: e.data.pid),
                          DataGridCell<String>(
                              columnName: 'title', value: e.data.meta.title),
                          DataGridCell<String>(
                              columnName: 'name', value: e.data.name),
                          DataGridCell<String>(
                              columnName: 'path', value: e.data.path),
                          DataGridCell<bool?>(
                              columnName: 'hidden',
                              value: e.data.meta.hidden),
                          DataGridCell<int>(
                              columnName: 'sort', value: e.data.sort),
                          DataGridCell<String>(
                              columnName: 'component',
                              value: e.data.component),
                          DataGridCell<DataMenusItem>(
                              columnName: 'buttons', value: e.data),
                        ]))
                    .toList();
              },
            );
          });
        },
      ),
    );
  }
}
